package edu.hawaii;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.wattdepot.client.WattDepotClient;
import edu.hawaii.halealohacli.Processor;

/**
 * HaleAloha.
 * 
 * @author John MacMillan
 * @author Mitchell Kupfer
 * @author Bryson Hori
 * 
 */
public class HaleAlohaCli {
  /**
   * Connects to HaleAloha WattDepot server and prompts for user input.
   * 
   * @param args should be empty.
   */
  public static void main(String[] args) {

    // URL to the WattDepot server
    String url = "http://server.wattdepot.org:8190/wattdepot/";

    // Create a WattDepotClient with URL
    WattDepotClient client = new WattDepotClient(url);

    // Check to make sure a connection can be made.
    // If no connection, then exit right now.
    if (client.isHealthy()) {
      System.out.format("Connected successfully to: %s\n\n", url);
    }

    else {
      System.out.format("Could not connect to: %s\n\n", url);
      return;
    }

    // declaration of new instance of a processor.
    Processor processor = new Processor();

    // input variable that will store user input
    String input = null;

    // make limit 1000 because infinite loop causes findbugs error.
    // this is the main prompt for the program.
    for (int i = 0; i < 100; i++) {

      System.out.println(">");

      // input buffer
      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

      // try to get input
      try {
        input = reader.readLine();

        if (input != null) {

          // quit is handled in main.
          if (input.equalsIgnoreCase("quit")) {
            return;
          }

          // passes input to the processor
          processor.parseInput(input, client);
        }
      }

      catch (Exception e) {
        System.out.println("Please input a valid command");
      }

    }

  }

}